package com.bd.scm.module.depCardApplication.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bd.scm.domain.dto.aggrid.AgGridRequest;
import com.bd.scm.domain.dto.aggrid.AgGridResponse;
import com.bd.scm.domain.dto.depCardApplication.BdDepCardUseDto;
import com.bd.scm.module.common.BaseController;
import com.bd.scm.module.depCardApplication.service.IBdDepCardUseService;
import com.bd.scm.module.utils.ResultUtil;
import com.bd.utils.vo.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.net.InetAddress;
import java.net.UnknownHostException;

/**部门卡使用情况 Controller
 * @ClassName BdDepCardUseController
 * @Description TODO
 * @Author YH
 * @Date 2019/12/21 0021 10:35
 * @Version 1.0
 **/
@RestController
@RequestMapping("/api/bdDepCardUse")
public class BdDepCardUseController extends BaseController {

  private Logger log = LoggerFactory.getLogger("dataLogger");

  /**
   * 部门卡使用情况Service类
   */
  @Autowired
  private IBdDepCardUseService iBdDepCardUseService;


  /**
   * 获取分页查询所有的部门使用情况
   * @return
   */
  @PostMapping("findBdDepCardUseList")
  public Result findBdDepCardUseList(@RequestBody AgGridRequest request){
    log.info("params: {}", JSON.toJSON(request));
    Result<AgGridResponse> result = new Result<AgGridResponse>();
    //之后要根据传值，
    BdDepCardUseDto bdDepCardUseDto = JSON.parseObject(JSON.toJSONString(request.getData()),
        BdDepCardUseDto.class);
    Page<BdDepCardUseDto> page = getPage(request);;
    IPage<BdDepCardUseDto> pageList = this.iBdDepCardUseService.findBdDepCardUseList(page, bdDepCardUseDto);
    AgGridResponse<BdDepCardUseDto> response = getResponse(request, pageList, page);
    log.info("totalPages: {}", pageList.getPages());
    result.setSuccess(true);
    result.setResult(response);
    return result;
  }

  /**
   * 获取人员列表
   * @return
   */
  @PostMapping("findUserList")
  public Result findUserList(@RequestBody BdDepCardUseDto depCardUseDto) {
    Result result = null;
    try {
      result = Result.ok(this.iBdDepCardUseService.findUserList(depCardUseDto.getUserName()));
    } catch (Exception e) {
      e.printStackTrace();
      result = Result.error("程序内部错误");
    }
    return result;
  }

  /**
   * 获取部门卡列表
   * @return
   */
  @PostMapping("findDapCardList")
  public Result findDapCardList(@RequestBody BdDepCardUseDto bdDepCardUseDto) {
    Result result = null;
    try {
      result = Result.ok(this.iBdDepCardUseService.findDapCardList(bdDepCardUseDto));
    } catch (Exception e) {
      e.printStackTrace();
      result = Result.error("程序内部错误");
    }
    return result;
  }

  /**
   * 添加部门使用记录
   * @param request
   * @return
   * @throws UnknownHostException
   */
  @PostMapping("insertBdDepCardUse")
  @ResponseBody
  public Result insertBdDepCardUse(@RequestBody BdDepCardUseDto bdDepCardUseDto, HttpServletRequest request) throws UnknownHostException {
    String clientIp = InetAddress.getLocalHost().getHostAddress();
    String flag = this.iBdDepCardUseService.insertBdDepCardUse(bdDepCardUseDto,clientIp);
    if(null != flag){
      return ResultUtil.error(flag);
    }else{
      return ResultUtil.ok();
    }
  }

  /**
   * 修改部门使用记录
   * @param request
   * @return
   * @throws UnknownHostException
   */
  @PostMapping("updateBdDepCardUse")
  @ResponseBody
  public Result updateBdDepCardUse(@RequestBody BdDepCardUseDto bdDepCardUseDto, HttpServletRequest request) throws UnknownHostException {
    String clientIp = InetAddress.getLocalHost().getHostAddress();
    String flag = this.iBdDepCardUseService.updateBdDepCardUse(bdDepCardUseDto,clientIp);
    if(null != flag){
      return ResultUtil.error(flag);
    }else{
      return ResultUtil.ok();
    }
  }

  /**
   * 删除部门使用记录
   * @param request
   * @return
   * @throws UnknownHostException
   */
  @PostMapping("deleteBdDepCardUse")
  @ResponseBody
  public Result deleteBdDepCardUse(@RequestBody BdDepCardUseDto bdDepCardUseDto, HttpServletRequest request) throws UnknownHostException {
    String clientIp = InetAddress.getLocalHost().getHostAddress();
    String flag = this.iBdDepCardUseService.deleteBdDepCardUse(bdDepCardUseDto,clientIp);
    if(null != flag){
      return ResultUtil.error(flag);
    }else{
      return ResultUtil.ok();
    }
  }
}
